package top.lyc829.zzl_android.ui.thing.travel_detail;

import android.util.Pair;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.SonCommentBean;
import top.lyc829.zzl_android.logic.bean.SuperCommentBean;
import top.lyc829.zzl_android.logic.bean.TravelDetailBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class TravelDetailViewModel extends ViewModel {
    private MutableLiveData<Integer> travelIdLiveData;
    private MutableLiveData<TravelDetailBean> travelDetailLiveData;
    private MutableLiveData<List<SuperCommentBean>> superCommentsLiveData;
    private MutableLiveData<List<SonCommentBean>> sonCommentsLiveData;

    private MutableLiveData<Integer> isPraiseLiveData;
    private MutableLiveData<Integer> collectionStateLiveData;

    // first: 评论类型（1级 n级） second 评论id
    private MutableLiveData<Pair<Integer, Integer>> commentDeletePositionLiveData;


    //子评论数量变化时，通知一级评论刷新被评论的数量
    private MutableLiveData<Pair<Integer, Integer>> sonCommentChangePositionLiveData;

    public MutableLiveData<Pair<Integer, Integer>> getSonCommentChangePositionLiveData() {
        if (sonCommentChangePositionLiveData == null) {
            sonCommentChangePositionLiveData = new MutableLiveData<>();
        }
        return sonCommentChangePositionLiveData;
    }

    public MutableLiveData<Pair<Integer, Integer>> getCommentDeleteLiveData() {
        if (commentDeletePositionLiveData == null) {
            commentDeletePositionLiveData = new MutableLiveData<>();
            commentDeletePositionLiveData.setValue(new Pair<>(-1, -1));
        }
        return commentDeletePositionLiveData;
    }

    public MutableLiveData<Integer> getTravelIdLiveData() {
        if (travelIdLiveData == null) {
            travelIdLiveData = new MutableLiveData<>();
        }
        return travelIdLiveData;
    }

    public MutableLiveData<TravelDetailBean> getTravelDetailLiveData() {
        if (travelDetailLiveData == null) {
            travelDetailLiveData = new MutableLiveData<>();
        }
        return travelDetailLiveData;
    }

    public MutableLiveData<List<SuperCommentBean>> getSuperCommentsLiveData() {
        if (superCommentsLiveData == null) {
            superCommentsLiveData = new MutableLiveData<>();
        }
        return superCommentsLiveData;
    }

    public MutableLiveData<List<SonCommentBean>> getSonCommentsLiveData() {
        if (sonCommentsLiveData == null) {
            sonCommentsLiveData = new MutableLiveData<>();
        }
        return sonCommentsLiveData;
    }

    public MutableLiveData<Integer> getIsPraiseLiveData() {
        if (isPraiseLiveData == null) {
            isPraiseLiveData = new MutableLiveData<>();
        }
        return isPraiseLiveData;
    }

    public MutableLiveData<Integer> getCollectionStateLiveData() {
        if (collectionStateLiveData == null) {
            collectionStateLiveData = new MutableLiveData<>();
        }
        return collectionStateLiveData;
    }


    public void loadTravelDetail(int travelId) {
        Network.loadTravelDetail(travelId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<TravelDetailBean> resultBean = JsonUtilNew.fromJsonObject(
                        res.body().string(), TravelDetailBean.class);
                if (resultBean.getCode().equals(200)) {
                    getTravelDetailLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void loadSuperComments(int type, int typeId, int page) {
        Network.getSuperComments(typeId, page, type, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<List<SuperCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, SuperCommentBean.class);

                if (resultBean.getCode().equals(200)) {
                    getSuperCommentsLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void loadSonComments(int commentId, int page) {
        Network.getSonComments(commentId, page, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                Utils.Sout(resultStr);
                ResultBean<List<SonCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, SonCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    getSonCommentsLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void getPraiseRelationShip(int userId, int type, int typeId) {
        Network.getPraiseRelationShip(userId, type, typeId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                TypeToken<ResultBean<Integer>> typeToken = new TypeToken<ResultBean<Integer>>() {};
                ResultBean<Integer> resultBean = new Gson().fromJson(resultStr, typeToken.getType());

                if (resultBean.getCode().equals(200)) {
                    getIsPraiseLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }


    /**
     * 获取收藏关系
     * @param userId
     * @param travelId
     */
    public void getCollectionState(int userId, int travelId) {
        Network.getCollectionState(userId, travelId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<Integer> resultBean = JsonUtilNew.fromJsonObject(resultStr, Integer.class);

                if (resultBean.getCode().equals(200)) {
                    collectionStateLiveData.postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultStr));
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }



}
